package com.longtop.framework.sys.menu.web;

import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.menu.model.TSysMenu;
import com.longtop.framework.sys.menu.service.MenuService;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 系统管理-机构
 * 
 * @author SJH
 * 
 */
public class MenuListAction extends BaseAction {

	private MenuService service;

	private final String DATASET_MENU = "Dataset_Menu";

	public MenuListAction() {
		this.service = this.getBean(MenuService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		commonQuery(context, true);
	}

	/**
	 * 加载
	 * 
	 * @param context
	 */
	public void loadMenu(IActionContext context) {
		commonQuery(context, false);
	}

	/**
	 * private:读取菜单
	 * 
	 * @param context
	 * @param isRoot
	 *            是否延迟加载
	 */
	private void commonQuery(Object context, boolean isRoot) {
		Dataset dataset = this.getDataset(context, DATASET_MENU);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_MENU)) {
			return;
		}
		QueryConditions conditions = WebUtil.createQueryConditions();
		if (isRoot) {
			conditions.setConditionIsNull("parentMenuId");
		}
		String filter = dataset.getFilter();
		QueryResult queryResult = service.loadMenu(conditions, filter);

		try {
			BeanUtil.queryResultToDataset(queryResult, dataset, false, true);
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("加载菜单出错。");
		}
	}

	/**
	 * 提交保存（增，删，改）
	 * 
	 * @param context
	 */
	public void saveMenu(IActionContext context) {
		Dataset menuDataset = (Dataset) context.getDataset(DATASET_MENU);
		if (!WebUtil.validateDatasetChange(this, menuDataset, DATASET_MENU)) {// DatasetChange
			return;
		}
		try {
			Map changeMap = WebUtil.datasetToObjectList(menuDataset, "menuId", TSysMenu.class);
			Integer[] count = service.saveMenu(changeMap);
			String msg = WebUtil.getSubmitSuccessInfo(count[0].intValue(), count[1].intValue(), count[2].intValue());
			this.hintInfoMessage(msg);
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());// 自定义的错误
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存菜单失败。");
		}
	}

	/**
	 * 删除菜单
	 * 
	 * @param context
	 *            web上下文
	 * @throws Exception
	 */
	public void deleteMenu(IActionContext context) throws Exception {
		String id = context.getCommandParameterValue("id");
		try {
			SessionObject sessionObject = this.getSessionObject(context);
			service.deleteMenu(id, sessionObject);
		} catch (Exception e) {
			this.hintErrorMessage(e.getMessage());
		}
	}

	/**
	 * 判断是否有下级菜单，是否可以删除的判断条件。
	 * 
	 * @param context
	 *            WEB上下文
	 */
	public void hasChild(IActionContext context) {
		String id = context.getCommandParameterValue("id");
		// 设置查询条件列表
		QueryConditions queryConditions = WebUtil.createQueryConditions();
		queryConditions.setCondition("parentMenuId", QueryConditions.EQUAL, id);

		QueryResult queryResult = service.loadMenu(queryConditions, null);
		if (queryResult.getResultList().size() > 0) {
			context.addCommandParameter("hasChild", "true");
		} else {
			context.addCommandParameter("hasChild", "false");
		}
	}
}